package com.twotiger.and.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.twotiger.and.db.JpushNoticeOpenHelper;

public class JpushNoticeDao {
    public static final int MODE_SMS = 1;
    public static final int MODE_TEL = 0;
    public static final int MODE_ALL = 2;

    private JpushNoticeOpenHelper helper;
    private Context context;

    public JpushNoticeDao(Context context) {
        this.context = context;
        helper = new JpushNoticeOpenHelper(context);
    }

    /**
     * 增
     */
    public void addNotice(long notifactionId, String notifactionAlert, String notifactionTitle, String notifactionContent) {
        SQLiteDatabase sql = helper.getWritableDatabase();// 枷锁的数据库
        ContentValues values = new ContentValues();
        values.put("notifactionId", notifactionId);
        values.put("notifactionAlert", notifactionAlert);
        values.put("notifactionTitle", notifactionTitle);
        values.put("notifactionContent", notifactionContent);
        sql.insert(JpushNoticeOpenHelper.TABNAME, null, values);

        sql.close();
    }

    /**
     * 删   删除所有通知
     * 
     */
    public void deleteNotice() {
        SQLiteDatabase sql = helper.getWritableDatabase();// 加锁的数据库
        sql.delete(JpushNoticeOpenHelper.TABNAME, null, null);
        sql.close();
    }
    
    /**
     * 返回全部推送消息
     * 
     * @return
     */
    public List<String[]> queryAll() {
        List<String[]> jpushNoticeList = new ArrayList<String[]>();
        SQLiteDatabase sql = helper.getReadableDatabase();
        Cursor cursor = sql.query(JpushNoticeOpenHelper.TABNAME,
            new String[] { "notifactionId", "notifactionAlert", "notifactionTitle", "notifactionContent" }, null, null,
            null, null, "id desc");
        while (cursor.moveToNext()) {
            String notifactionId = cursor.getString(0);
            String notifactionAlert = cursor.getString(1);
            String notifactionTitle = cursor.getString(2);
            String notifactionContent = cursor.getString(3);
            String[] notifactionInfo = { notifactionId, notifactionAlert,notifactionTitle, notifactionContent };
            jpushNoticeList.add(notifactionInfo);
        }
        cursor.close();
        sql.close();
        return jpushNoticeList;

    }

    /**
     * 删   按通知的id来删除对应的消息
     * 
     */
    public void deleteNotice(String notifactionId) {
        SQLiteDatabase sql = helper.getWritableDatabase();// 加锁的数据库
        sql.delete(JpushNoticeOpenHelper.TABNAME, "notifactionId=?", new String[] { notifactionId });
        sql.close();
    }

    /**
     * 修改
     */
    public void updateNotice(String notifactionId, int notifactionContent) {
        SQLiteDatabase sql = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("notifactionContent", notifactionContent);
        // 表名
        sql.update(JpushNoticeOpenHelper.TABNAME, values, "notifactionId=?",
            new String[] { notifactionId });
    }

    

}
